package br.com.sovi.persistence;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.openinsula.arena.hibernate.entity.BaseEntity;
import org.springframework.util.ObjectUtils;

/**
 * @author João Galli
 *
 */
@Entity
public class Empresa extends BaseEntity<Long> {
	private static final long serialVersionUID = 1L;

	@Column(length = 100)
	private String descricao;

	@Column(length = 14)
	private String cnpj;
	
	@OneToMany(mappedBy = "empresa")
	private Set<Usuario> usuarios = new HashSet<Usuario>();

	public Empresa() {
	}

	@Override
	public String toString() {
		return String.format("[%s: id= %s], cnpj= %s", getClass().getName(), ObjectUtils.nullSafeToString(getId()),
				ObjectUtils.nullSafeToString(cnpj));
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null || !getClass().isAssignableFrom(obj.getClass())) {
			return false;
		}

		Empresa other = (Empresa) obj;

		return new EqualsBuilder().append(this.cnpj, other.cnpj).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(cnpj).toHashCode();
	}

	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	public String getCnpj() {
		return cnpj;
	}

	public void setCnpj(String cnpj) {
		this.cnpj = cnpj;
	}

	public void setUsuarios(Set<Usuario> usuarios) {
		this.usuarios = usuarios;
	}

	public Set<Usuario> getUsuarios() {
		return usuarios;
	}

}
